﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class AddressDetailControl : UserControl
    {
        private FATCASchema.Wrappers.Address _address;
        private AddressFix_Type _fixAddress;

        public FATCASchema.Wrappers.Address Address 
        { 
            get { return _address; }
            set 
            {
                _address = value;
                DataBind();
            }
        }

        public AddressDetailControl()
        {
            InitializeComponent();
        }

        public void Intitalize(FATCASchema.Wrappers.Address address)
        {
            Address = address;
        }

        private void DataBind()
        {
            LoadAddresses(Address.IsFreeTextAddress);

            if (_fixAddress != null)
            {
                txtBuildingID.DataBindings.Clear();
                txtBuildingID.DataBindings.Add("Text", _fixAddress, "BuildingIdentifier");

                txtCity.DataBindings.Clear();
                txtCity.DataBindings.Add("Text", _fixAddress, "City");

                txtCountrySubentity.DataBindings.Clear();
                txtCountrySubentity.DataBindings.Add("Text", _fixAddress, "CountrySubentity");

                txtDistrictName.DataBindings.Clear();
                txtDistrictName.DataBindings.Add("Text", _fixAddress, "DistrictName");

                txtFloorID.DataBindings.Clear();
                txtFloorID.DataBindings.Add("Text", _fixAddress, "FloorIdentifier");

                txtPOBox.DataBindings.Clear();
                txtPOBox.DataBindings.Add("Text", _fixAddress, "POB");

                txtPostCode.DataBindings.Clear();
                txtPostCode.DataBindings.Add("Text", _fixAddress, "PostCode");

                txtStreet.DataBindings.Clear();
                txtStreet.DataBindings.Add("Text", _fixAddress, "Street");

                txtSuiteID.DataBindings.Clear();
                txtSuiteID.DataBindings.Add("Text", _fixAddress, "SuiteIdentifier");
            }



            //txtContact.DataBindings.Clear();
            //txtContact.DataBindings.Add("Text", _messageSpec, "Contact");

            cmbCountry.DataBindings.Clear();
            cmbCountry.DataSource = Enum.GetValues(typeof(CountryCode_Type));
            cmbCountry.DataBindings.Add("SelectedItem", _address, "Country");

            var addType = _address.AddressType;
            cmbAddressType.DataBindings.Clear();
            cmbAddressType.DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(OECDLegalAddressType_EnumType));
            cmbAddressType.ValueMember = "Key";
            cmbAddressType.DisplayMember = "Value";
            cmbAddressType.DataBindings.Add("SelectedItem", _address, "AddressType");
            cmbAddressType.SelectedValue = addType;
        }

        private void cmbFixedFree_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbFixedFree.Text == "Free")
                Address.IsFreeTextAddress = true;
            else
                Address.IsFreeTextAddress = false;

            DataBind();
        }

        private void LoadAddresses(bool isFree)
        {
            _fixAddress = null;

            if (_address.FATCAAddress.Items != null && _address.FATCAAddress.Items.Count() > 0)
            {
                foreach (var add in _address.FATCAAddress.Items)
                {
                    if (add is AddressFix_Type && _fixAddress == null)
                        _fixAddress = (AddressFix_Type)add;
                }
            }

            if (!isFree && _fixAddress == null)
                _fixAddress = new AddressFix_Type();

            if (isFree)
            {
                _address.IsFreeTextAddress = true;
                grpFixed.Visible = false;
                
            }
            else
            {
                _address.IsFreeTextAddress = false;
                grpFixed.Visible = true;
            }
         }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ParentForm.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Address.FATCAAddress.CountryCode = Address.Country;
            if (cmbAddressType.SelectedItem != null && cmbAddressType.SelectedValue is OECDLegalAddressType_EnumType)
                Address.FATCAAddress.legalAddressType = (OECDLegalAddressType_EnumType)cmbAddressType.SelectedValue;            
            else
                Address.FATCAAddress.legalAddressType = Address.AddressType;            

            if (_fixAddress == null)
            {
                Address.FATCAAddress.Items = new object[] { txtFree.Text };
            }
            else
            {
                if (txtFree.Text.Trim() == string.Empty)
                    Address.FATCAAddress.Items = new object[] { _fixAddress };
                else
                    Address.FATCAAddress.Items = new object[] { _fixAddress, txtFree.Text };
            }

            Address.UpdateFriendlyFields();
            this.ParentForm.DialogResult = DialogResult.OK;
        }

        private void cmbAddressType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAddressType.SelectedItem != null)
            {
                Address.AddressType = (OECDLegalAddressType_EnumType)cmbAddressType.SelectedValue;
                Address.FATCAAddress.legalAddressType = (OECDLegalAddressType_EnumType)cmbAddressType.SelectedValue;
            }
        }

    }
}
